package com.jr.dao.impl;

import com.jr.dao.AddressDao;
import com.jr.entity.Address;
import com.jr.util.DAO;

import java.util.List;

public class AddressDaoImpl extends DAO<Address> implements AddressDao {

    /**
     * 添加地址
     *
     * @param address
     * @return
     * @throws Exception
     */
    @Override
    public int addAddress(Address address) throws Exception {
        String sql = "insert into address (enterprise_id,addressee,phone,area,address_detail) values (?,?,?,?,?)";
        return update(sql, address.getEnterprise_id(), address.getAddressee(), address.getPhone(), address.getArea(), address.getAddress_detail());
    }

    /**
     * 根据企业id查询所有相关的地址
     *
     * @param enterpriseId
     * @return
     * @throws Exception
     */
    @Override
    public List<Address> getAddress(Integer enterpriseId) throws Exception {
        String sql = "SELECT id,addressee,phone,area,address_detail,default_status from address where exist=1 AND enterprise_id = ?";
        return getForList(sql, enterpriseId);
    }

    /**
     * 根据id更新Exist状态,用作删除地址
     *
     * @param addressId
     * @return
     * @throws Exception
     */
    @Override
    public int updateAddressExist(Integer addressId) throws Exception {
        String sql = "update address set exist = 0 where id = ?";
        return update(sql, addressId);
    }

    /**
     * 根据id修改地址
     * @param address
     * @return
     * @throws Exception
     */
    @Override
    public int saveAddressById(Address address) throws Exception {
        String sql = "update address set addressee=?,phone=?,area=?,address_detail=? where id = ?";
        return update(sql,address.getAddressee(),address.getPhone(),address.getArea(),address.getAddress_detail(),address.getId());
    }

    /**
     * 根据id设置地址默认
     * @param addressId
     * @return
     * @throws Exception
     */
    @Override
    public int setAddressDefaultById(Integer addressId) throws Exception {
        String sql = "update address set default_status = 0 where id = ?";
        return update(sql,addressId);
    }

    @Override
    public int setAllAddressDefault() throws Exception {
        String sql = "update address set default_status = 1";
        return update(sql);
    }
}
